package com.yxq.mdt.app.modules.goodsdetail.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.base.RespBase;
import com.yxq.mdt.app.base.ibase.IBaseModel;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.goodsdetail.bean.CollectGoodsBean;
import com.yxq.mdt.app.modules.goodsdetail.bean.GoodsCommentBean;
import com.yxq.mdt.app.modules.goodsdetail.bean.GoodsDetailBean;
import com.yxq.mdt.app.modules.login.bean.UserBean;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class GoodsModel implements IBaseModel {

    public void getGoods(String id, ApiModel.CommonCallback<GoodsDetailBean> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(GoodsApi.class)
                .getGoods(userBean != null ? userBean.getAccess_token() : "", id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (!TextUtils.isEmpty(respBase.getData())) {
                            callback.onSuccess(JSON.parseObject(respBase.getData(), GoodsDetailBean.class));
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void goodsRecommend(String id, ApiModel.CommonCallback<MallGoodsBean> callback) {
        RetrofitFactory.getRetrofit().create(GoodsApi.class)
                .getGoodsRecommend(id, 1)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<MallGoodsBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<MallGoodsBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void favoriteAdd(String id, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(GoodsApi.class)
                .favoriteAdd(id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void favoriteRemove(String id, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(GoodsApi.class)
                .favoriteRemove(id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void getCommentList(String id, int page, ApiModel.CommonCallback<GoodsCommentBean> callback) {
        RetrofitFactory.getRetrofit().create(GoodsApi.class)
                .getCommentList(id, page)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<GoodsCommentBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<GoodsCommentBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void addCart(String id, int page, String attr, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(GoodsApi.class)
                .addCart(id, page, attr)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public static void favoriteList(int page, ApiModel.CommonCallback<CollectGoodsBean> callback) {
        RetrofitFactory.getRetrofit().create(GoodsApi.class)
                .favoriteList(Constant.getToken(),page)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<CollectGoodsBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<CollectGoodsBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }
    @Override
    public void onDestroy() {

    }
}
